//
//  QRCodeVC.swift
//  CooperSwift_Example
//
//  Created by 计海峰 on 2021/10/15.
//  Copyright © 2021 CocoaPods. All rights reserved.
//

import UIKit
import EFQRCode

class QRCodeVC: UIViewController {

    @IBOutlet weak var qrCodeOne: UIImageView!
    @IBOutlet weak var qrCodeTwo: UIImageView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        if let image = EFQRCode.generate(
            for: "https://github.com/EFPrefix/EFQRCode",
            icon: UIImage(named: "settings")?.cgImage
        ) {
            /// 生成二维码
            self.qrCodeOne.image = UIImage(cgImage: image)
            print("Create QRCode image success \(image)")
            
            /// 解析图片二维码
            let codes = EFQRCode.recognize(image)
            if !codes.isEmpty {
                print("There are \(codes.count) codes")
                for (index, code) in codes.enumerated() {
                    print("The content of QR Code \(index) is \(code).")
                }
            } else {
                print("There is no QR Codes in testImage.")
            }
            
        } else {
            print("Create QRCode image failed!")
        }
        
        /// 解析图片二维码
        if let testImage = UIImage.init(named: "test")?.cgImage {
            let codes = EFQRCode.recognize(testImage)
            if !codes.isEmpty {
                print("There are \(codes.count) codes")
                for (index, code) in codes.enumerated() {
                    print("The content of QR Code \(index) is \(code).")
                }
            } else {
                print("There is no QR Codes in testImage.")
            }
        }
        
//        if let testImage = image as? CGImage {
//            let codes = EFQRCode.recognize(testImage)
//            if !codes.isEmpty {
//                print("There are \(codes.count) codes")
//                for (index, code) in codes.enumerated() {
//                    print("The content of QR Code \(index) is \(code).")
//                }
//            } else {
//                print("There is no QR Codes in testImage.")
//            }
//        }
        
        // Do any additional setup after loading the view.
    }


    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
